package com.example.diary;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.data.DBManger;
import com.example.timecalculous.R;
import com.example.data.TimeData;

public class DiaryHistories extends Activity {
    DiaryAdapter diaryAdapter;
    Cursor mCurse;
    TimeData timeData;
    DBManger dbManger;
    int TIME_ID=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        timeData=new TimeData(this);
        dbManger=new DBManger();
        mCurse=select1();
        final AlertDialog.Builder dialog=new AlertDialog.Builder(DiaryHistories.this);
        final ListView listView= (ListView) findViewById(R.id.listview1);
        diaryAdapter=new DiaryAdapter(this,mCurse);
        listView.setAdapter(diaryAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                mCurse.moveToPosition(position);
                TIME_ID=mCurse.getInt(0);
                Intent intent=new Intent(DiaryHistories.this,DiaryXiugai.class);
                Bundle bundle=new Bundle();
                bundle.putInt("diaryid",TIME_ID);
                intent.putExtras(bundle);
                startActivity(intent);
                DiaryHistories.this.finish();
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                timeData=new TimeData(DiaryHistories.this);
                mCurse.moveToPosition(position);
                TIME_ID=mCurse.getInt(0);
                dialog.setTitle("删除");
                dialog.setIcon(R.drawable.ic_launcher);
                dialog.setPositiveButton("确定", new  DialogInterface.OnClickListener() {

                    @SuppressWarnings("deprecation")
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        if(TIME_ID==0){
                            return;
                        }
                        delete1(TIME_ID);
                        mCurse.requery();
                        listView.setAdapter(diaryAdapter);
                        timeData.close();
                        diaryAdapter.notifyDataSetChanged();
                    }
                });
                dialog.create();
                dialog.show();
                return true;
            }
        });
    }
    //查找操作1
    public Cursor select1() {
        SQLiteDatabase db = timeData.getWritableDatabase();
        Cursor cursor= db.query(dbManger.TABLE_NAME1, null, null, null, null, null, null);
        return cursor;
    }

    //删除操作
    public void delete1(int id)
    {
        SQLiteDatabase db = timeData.getWritableDatabase();
        String where =dbManger.TIME_ID1 + " = ?";
        String[] whereValue ={ Integer.toString(id) };
        db.delete(dbManger.TABLE_NAME1, where, whereValue);
    }
}
